﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Lenovo.CFI.BLL;
using Lenovo.CFI.Common;
using Lenovo.CFI.Web.Helper;
using Lenovo.CFI.BLL.Sys;

namespace Lenovo.CFI.Web.VP.MyIdea
{
    public partial class MyIdeaEdit : System.Web.UI.UserControl
    {
        IdeaBl ideaBl = new IdeaBl();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int? id = UrlHelper.GetQueryStringToInt("id");
                Idea idea = null;
                if (id.HasValue) idea = ideaBl.GetIdeaById(id.Value);
                if (idea == null) ErrorHelper.RaiseNoValueError();
                if (idea.Owner != UserHelper.UserName) ErrorHelper.RaiseNoPermError();

                ViewState["ID"] = idea.ID;

                Topic topic = new TopicBl().GetTopicByID(idea.TopicID);
                this.TxtTopic.Text = topic.Title;
                this.TxtTitle.Text = idea.Title;

                BindHelper.BindIdeaType(this.DdlIdeaType, "Select...", "-1", idea.Type, false);
                BindHelper.BindIdeaType(this.CbRelatedType, null, null, null, false);
                foreach (ListItem item in this.CbRelatedType.Items)
                {
                    if (Array.Exists(idea.RelatedType.List, (t => t == item.Value)))
                        item.Selected = true;
                }

                BindHelper.BindKeyword(DdlKeyword1, "Select...", "-1", null, true);
                BindHelper.BindKeyword(DdlKeyword2, "Select...", "-1", null, true);
                BindHelper.BindKeyword(DdlKeyword3, "Select...", "-1", null, true);
                if (idea.Keywords.List.Length > 0)
                {
                    ListItem item = this.DdlKeyword1.Items.FindByValue(idea.Keywords.List[0]);
                    if (item != null) item.Selected = true;
                }
                if (idea.Keywords.List.Length > 1)
                {
                    ListItem item = this.DdlKeyword2.Items.FindByValue(idea.Keywords.List[1]);
                    if (item != null) item.Selected = true;
                }
                if (idea.Keywords.List.Length > 2)
                {
                    ListItem item = this.DdlKeyword3.Items.FindByValue(idea.Keywords.List[2]);
                    if (item != null) item.Selected = true;
                }
                this.TxtFreeKeyword.Text = idea.FreewordsText;

                this.TxtBackground.Text = idea.Backgroud;
                this.TxtDescription.Text = idea.Description;
                this.TxtValue.Text = idea.Values;


                AttachmentBl attachmentBl = new AttachmentBl();
                if (idea.PptFileID.HasValue)
                {
                    this.AfuFilePpt.SetFile(idea.PptFileID.Value, attachmentBl.GetAttachmentByID(idea.PptFileID.Value).Title,
                          AttachmentUrl.GetFileUrl(idea.PptFileID.Value, true));
                }
                else
                    this.AfuFilePpt.SetFile(null, null, null);

                if (idea.Pic1FileID.HasValue)
                {
                    this.AfuFilePic1.SetFile(idea.Pic1FileID.Value, attachmentBl.GetAttachmentByID(idea.Pic1FileID.Value).Title,
                          AttachmentUrl.GetFileUrl(idea.Pic1FileID.Value, true));
                }
                else
                    this.AfuFilePic1.SetFile(null, null, null);

                if (idea.Pic2FileID.HasValue)
                {
                    this.AfuFilePic2.SetFile(idea.Pic2FileID.Value, attachmentBl.GetAttachmentByID(idea.Pic2FileID.Value).Title,
                          AttachmentUrl.GetFileUrl(idea.Pic2FileID.Value, true));
                }
                else
                    this.AfuFilePic2.SetFile(null, null, null);

                if (idea.Pic3FileID.HasValue)
                {
                    this.AfuFilePic3.SetFile(idea.Pic3FileID.Value, attachmentBl.GetAttachmentByID(idea.Pic3FileID.Value).Title,
                          AttachmentUrl.GetFileUrl(idea.Pic3FileID.Value, true));
                }
                else
                    this.AfuFilePic3.SetFile(null, null, null);

                BindHelper.BindUser(this.DdlApprover, "Select...", "-1", idea.Approver, UserHelper.TeamID);

                this.TxtOwner.Text = UserHelper.UserName;


                if (topic.Status != TopicStatus.Start ||
                    DateTime.Now < topic.StartTime)
                {
                    MessageHelper.Show("The topic does not start or has been closed!", this.Page);

                    this.BtnSave.Enabled = false;
                    this.BtnSubmit.Enabled = false;
                }
            }
        }

        protected void BtnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                this.Save(true);
                MessageHelper.Show("Success!", this.Page);
            }
            catch (ApplicationException ex)
            {
                MessageHelper.RegShowJS(ex.Message, this.Page);
            }

            this.BtnSubmit.Enabled = false;
            this.BtnSave.Enabled = false;
        }

        protected void BtnSave_Click(object sender, EventArgs e)
        {
            try
            {
                this.Save(false);
                MessageHelper.Show("Success!", this.Page);
            }
            catch (ApplicationException ex)
            {
                MessageHelper.RegShowJS(ex.Message, this.Page);
            }
        }


        private void Save(bool submit)
        {
            List<string> relatedTypes = new List<string>();
            foreach (ListItem item in this.CbRelatedType.Items)
            {
                if (item.Selected)
                    relatedTypes.Add(item.Value);
            }

            List<string> keywords = new List<string>();
            if (this.DdlKeyword1.SelectedValue != "-1")
                keywords.Add(this.DdlKeyword1.SelectedValue);
            if (this.DdlKeyword2.SelectedValue != "-1")
                keywords.Add(this.DdlKeyword2.SelectedValue);
            if (this.DdlKeyword3.SelectedValue != "-1")
                keywords.Add(this.DdlKeyword3.SelectedValue);

            string freewords = null;
            if (!string.IsNullOrEmpty(TxtFreeKeyword.Text.Trim()))
                freewords = TxtFreeKeyword.Text.Trim();

            string approver = null;
            if (this.DdlApprover.SelectedIndex != 0)
                approver = this.DdlApprover.SelectedValue;

            new IdeaBl().Update((int)ViewState["ID"], this.TxtTitle.Text.Trim(), 
                this.DdlIdeaType.SelectedValue, 
                relatedTypes, keywords, freewords, 
                this.TxtBackground.Text.Trim(), this.TxtDescription.Text.Trim(), this.TxtValue.Text.Trim(),
                this.AfuFilePpt.FinalFileID, this.AfuFilePic1.FinalFileID, this.AfuFilePic2.FinalFileID, this.AfuFilePic3.FinalFileID,
                approver, submit);
        }

    }
}